package com.example.base.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.base.R;
import com.example.base.activity.HistoryActivity;
import com.example.base.bean.HistoryBean;

import java.util.ArrayList;
import java.util.List;

public class HistoryViewAdapter extends BaseAdapter {
  private final List<HistoryBean> dataList = new ArrayList<>();
  private final Context context;


  public HistoryViewAdapter(HistoryActivity historyActivity, List<HistoryBean> historyBeans) {
    this.dataList.addAll(historyBeans);
    this.context = historyActivity;
  }

  @Override
  public int getCount() {
    return dataList.size();
  }

  @Override
  public Object getItem(int position) {
    return null;
  }

  @Override
  public long getItemId(int position) {
    return position;
  }

  @SuppressLint("InflateParams")
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    MyViewHolder myViewHolder;
    if (convertView == null) {
      myViewHolder = new MyViewHolder();
      LayoutInflater inflater = LayoutInflater.from(context);
      convertView = inflater.inflate(R.layout.list_history, null, true);
      myViewHolder.tv_time = convertView.findViewById(R.id.text_history_time);
      myViewHolder.tv_title = convertView.findViewById(R.id.text_history_title);
      myViewHolder.tv_text = convertView.findViewById(R.id.text_history_text);
      convertView.setTag(myViewHolder);
    } else {
      myViewHolder = (MyViewHolder) convertView.getTag();
    }
    HistoryBean historyBean = dataList.get(position);
    myViewHolder.tv_time.setText(historyBean.getTime());
    myViewHolder.tv_title.setText(historyBean.getTitle());
    myViewHolder.tv_text.setText(historyBean.getText());
    return convertView;
  }

  public void setData(ArrayList<HistoryBean> historyBeans) {
    dataList.clear();
    dataList.addAll(historyBeans);
  }


  static class MyViewHolder {
    TextView tv_time;
    TextView tv_title;
    TextView tv_text;
  }
}
